home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / QB8M3G (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  10.0 KB  |  307 lines

  1. package com.sun.java.swing.text;
  2.  
  3. import com.sun.java.swing.SwingUtilities;
  4. import com.sun.java.swing.event.ChangeListener;
  5. import java.awt.Font;
  6. import java.awt.FontMetrics;
  7. import java.awt.Toolkit;
  8. import java.io.IOException;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.io.Serializable;
  12. import java.util.Enumeration;
  13. import java.util.Hashtable;
  14. import java.util.Vector;
  15.  
  16. public class StyleContext implements Serializable, AbstractDocument.AttributeContext {
  17.    private static StyleContext defaultContext;
  18.    public static final String DEFAULT_STYLE = "default";
  19.    private static Hashtable freezeKeyMap;
  20.    private static Hashtable thawKeyMap;
  21.    private Style styles = new NamedStyle(this, (Style)null);
  22.    private transient FontKey fontSearch = new FontKey((String)null, 0, 0);
  23.    private transient Hashtable fontTable = new Hashtable();
  24.    private transient Hashtable attributesPool = new Hashtable();
  25.    private transient MutableAttributeSet search = new SimpleAttributeSet();
  26.    private int unusedSets;
  27.    static final int THRESHOLD = 9;
  28.    static Class class$com$sun$java$swing$event$ChangeListener;
  29.  
  30.    public StyleContext() {
  31.       this.addStyle("default", (Style)null);
  32.    }
  33.  
  34.    public synchronized AttributeSet addAttribute(AttributeSet old, Object name, Object value) {
  35.       if (old.getAttributeCount() + 1 <= this.getCompressionThreshold()) {
  36.          this.search.removeAttributes(this.search);
  37.          this.search.addAttributes(old);
  38.          this.search.addAttribute(name, value);
  39.          this.reclaim(old);
  40.          return this.getImmutableUniqueSet();
  41.       } else {
  42.          MutableAttributeSet ma = this.getMutableAttributeSet(old);
  43.          ma.addAttribute(name, value);
  44.          return ma;
  45.       }
  46.    }
  47.  
  48.    public synchronized AttributeSet addAttributes(AttributeSet old, AttributeSet attr) {
  49.       if (old.getAttributeCount() + attr.getAttributeCount() <= this.getCompressionThreshold()) {
  50.          this.search.removeAttributes(this.search);
  51.          this.search.addAttributes(old);
  52.          this.search.addAttributes(attr);
  53.          this.reclaim(old);
  54.          return this.getImmutableUniqueSet();
  55.       } else {
  56.          MutableAttributeSet ma = this.getMutableAttributeSet(old);
  57.          ma.addAttributes(attr);
  58.          return ma;
  59.       }
  60.    }
  61.  
  62.    public void addChangeListener(ChangeListener l) {
  63.       this.styles.addChangeListener(l);
  64.    }
  65.  
  66.    public Style addStyle(String nm, Style parent) {
  67.       Style style = new NamedStyle(this, nm, parent);
  68.       if (nm != null) {
  69.          this.styles.addAttribute(nm, style);
  70.       }
  71.  
  72.       return style;
  73.    }
  74.  
  75.    protected int getCompressionThreshold() {
  76.       return 9;
  77.    }
  78.  
  79.    public static final StyleContext getDefaultStyleContext() {
  80.       if (defaultContext == null) {
  81.          defaultContext = new StyleContext();
  82.       }
  83.  
  84.       return defaultContext;
  85.    }
  86.  
  87.    public AttributeSet getEmptySet() {
  88.       return SimpleAttributeSet.EMPTY;
  89.    }
  90.  
  91.    public Font getFont(AttributeSet attr) {
  92.       int style = 0;
  93.       if (StyleConstants.isBold(attr)) {
  94.          style |= 1;
  95.       }
  96.  
  97.       if (StyleConstants.isItalic(attr)) {
  98.          style |= 2;
  99.       }
  100.  
  101.       String family = StyleConstants.getFontFamily(attr);
  102.       int size = StyleConstants.getFontSize(attr);
  103.       return this.getFont(family, style, size);
  104.    }
  105.  
  106.    public Font getFont(String family, int style, int size) {
  107.       this.fontSearch.setValue(family, style, size);
  108.       Font f = (Font)this.fontTable.get(this.fontSearch);
  109.       if (f == null) {
  110.          f = new Font(family, style, size);
  111.          FontKey key = new FontKey(family, style, size);
  112.          this.fontTable.put(key, f);
  113.       }
  114.  
  115.       return f;
  116.    }
  117.  
  118.    public FontMetrics getFontMetrics(Font f) {
  119.       return Toolkit.getDefaultToolkit().getFontMetrics(f);
  120.    }
  121.  
  122.    AttributeSet getImmutableUniqueSet() {
  123.       SmallAttributeSet key = new SmallAttributeSet(this, this.search);
  124.       SmallAttributeSet a = (SmallAttributeSet)this.attributesPool.get(key);
  125.       if (a == null) {
  126.          a = key;
  127.          this.attributesPool.put(key, key);
  128.       }
  129.  
  130.       ++a.nrefs;
  131.       return a;
  132.    }
  133.  
  134.    MutableAttributeSet getMutableAttributeSet(AttributeSet a) {
  135.       return (MutableAttributeSet)(a instanceof MutableAttributeSet ? (MutableAttributeSet)a : new SimpleAttributeSet(a));
  136.    }
  137.  
  138.    public Style getStyle(String nm) {
  139.       return (Style)this.styles.getAttribute(nm);
  140.    }
  141.  
  142.    public Enumeration getStyleNames() {
  143.       return this.styles.getAttributeNames();
  144.    }
  145.  
  146.    public static void readAttributeSet(ObjectInputStream in, MutableAttributeSet a) throws ClassNotFoundException, IOException {
  147.       int n = in.readInt();
  148.  
  149.       for(int i = 0; i < n; ++i) {
  150.          Object key = in.readObject();
  151.          Object value = in.readObject();
  152.          Object staticKey = thawKeyMap.get(key);
  153.          if (staticKey != null) {
  154.             key = staticKey;
  155.          }
  156.  
  157.          a.addAttribute(key, value);
  158.       }
  159.  
  160.    }
  161.  
  162.    private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
  163.       this.fontSearch = new FontKey((String)null, 0, 0);
  164.       this.fontTable = new Hashtable();
  165.       this.search = new SimpleAttributeSet();
  166.       this.attributesPool = new Hashtable();
  167.       s.defaultReadObject();
  168.    }
  169.  
  170.    public void reclaim(AttributeSet a) {
  171.       if (a instanceof SmallAttributeSet) {
  172.          SmallAttributeSet sa = (SmallAttributeSet)a;
  173.          --sa.nrefs;
  174.          if (sa.nrefs <= 0) {
  175.             ++this.unusedSets;
  176.             if (this.unusedSets > 10 && this.unusedSets > this.attributesPool.size() / 10) {
  177.                if (SwingUtilities.isEventDispatchThread()) {
  178.                   this.removeUnusedSets();
  179.                } else {
  180.                   Runnable callRemoveUnused = new 1(this);
  181.                   SwingUtilities.invokeLater(callRemoveUnused);
  182.                }
  183.             }
  184.          }
  185.       }
  186.  
  187.    }
  188.  
  189.    public static void registerStaticAttributeKey(Object key) {
  190.       String ioFmt = key.getClass().getName() + "." + key.toString();
  191.       if (freezeKeyMap == null) {
  192.          freezeKeyMap = new Hashtable();
  193.          thawKeyMap = new Hashtable();
  194.       }
  195.  
  196.       freezeKeyMap.put(key, ioFmt);
  197.       thawKeyMap.put(ioFmt, key);
  198.    }
  199.  
  200.    public synchronized AttributeSet removeAttribute(AttributeSet old, Object name) {
  201.       if (old.getAttributeCount() - 1 <= this.getCompressionThreshold()) {
  202.          this.search.removeAttributes(this.search);
  203.          this.search.addAttributes(old);
  204.          this.search.removeAttribute(name);
  205.          this.reclaim(old);
  206.          return this.getImmutableUniqueSet();
  207.       } else {
  208.          MutableAttributeSet ma = this.getMutableAttributeSet(old);
  209.          ma.removeAttribute(name);
  210.          return ma;
  211.       }
  212.    }
  213.  
  214.    public synchronized AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs) {
  215.       if (old.getAttributeCount() <= this.getCompressionThreshold()) {
  216.          this.search.removeAttributes(this.search);
  217.          this.search.addAttributes(old);
  218.          this.search.removeAttributes(attrs);
  219.          this.reclaim(old);
  220.          return this.getImmutableUniqueSet();
  221.       } else {
  222.          MutableAttributeSet ma = this.getMutableAttributeSet(old);
  223.          ma.removeAttributes(attrs);
  224.          return ma;
  225.       }
  226.    }
  227.  
  228.    public synchronized AttributeSet removeAttributes(AttributeSet old, Enumeration names) {
  229.       if (old.getAttributeCount() <= this.getCompressionThreshold()) {
  230.          this.search.removeAttributes(this.search);
  231.          this.search.addAttributes(old);
  232.          this.search.removeAttributes(names);
  233.          this.reclaim(old);
  234.          return this.getImmutableUniqueSet();
  235.       } else {
  236.          MutableAttributeSet ma = this.getMutableAttributeSet(old);
  237.          ma.removeAttributes(names);
  238.          return ma;
  239.       }
  240.    }
  241.  
  242.    public void removeChangeListener(ChangeListener l) {
  243.       this.styles.removeChangeListener(l);
  244.    }
  245.  
  246.    public void removeStyle(String nm) {
  247.       this.styles.removeAttribute(nm);
  248.    }
  249.  
  250.    synchronized void removeUnusedSets() {
  251.       Vector rmList = new Vector();
  252.       Enumeration sets = this.attributesPool.keys();
  253.  
  254.       while(sets.hasMoreElements()) {
  255.          SmallAttributeSet set = (SmallAttributeSet)sets.nextElement();
  256.          if (set.nrefs <= 0) {
  257.             rmList.addElement(set);
  258.          }
  259.       }
  260.  
  261.       sets = rmList.elements();
  262.  
  263.       while(sets.hasMoreElements()) {
  264.          this.attributesPool.remove(sets.nextElement());
  265.       }
  266.  
  267.       this.unusedSets = 0;
  268.    }
  269.  
  270.    public String toString() {
  271.       this.removeUnusedSets();
  272.       String s = "";
  273.  
  274.       SmallAttributeSet set;
  275.       for(Enumeration sets = this.attributesPool.keys(); sets.hasMoreElements(); s = s + set + "\n") {
  276.          set = (SmallAttributeSet)sets.nextElement();
  277.       }
  278.  
  279.       return s;
  280.    }
  281.  
  282.    public static void writeAttributeSet(ObjectOutputStream out, AttributeSet a) throws IOException {
  283.       int n = a.getAttributeCount();
  284.       out.writeInt(n);
  285.       Enumeration keys = a.getAttributeNames();
  286.  
  287.       while(keys.hasMoreElements()) {
  288.          Object key = keys.nextElement();
  289.          if (key instanceof Serializable) {
  290.             out.writeObject(key);
  291.          } else {
  292.             Object ioFmt = freezeKeyMap.get(key);
  293.             out.writeObject(ioFmt);
  294.          }
  295.  
  296.          Object value = a.getAttribute(key);
  297.          out.writeObject(value);
  298.       }
  299.  
  300.    }
  301.  
  302.    private void writeObject(ObjectOutputStream s) throws IOException {
  303.       this.removeUnusedSets();
  304.       s.defaultWriteObject();
  305.    }
  306. }
  307.